package com.bc.web.controller.admin;

import java.math.BigDecimal;
import java.util.List;

import com.bc.web.config.SystemEnum;
import com.bc.web.model.Channel;
import com.bc.web.model.ClearingCollect;
import com.bc.web.model.WxClear;
import com.fs.common.extend.FSController;
import com.fs.common.extend.FSSearchCondition;
import com.fs.common.util.FSDate;
import com.fs.common.util.FSIDGenerate;
import com.jfinal.plugin.activerecord.Page;

public class ClearingCollectController extends FSController {
	public void collectRiskSearch() {
		FSSearchCondition sc = super.getSearchCondition();
		sc.setParameter("isRisk", "true");
		Page<ClearingCollect> pageResult = ClearingCollect.dao.search(sc);
		setPageResultToRequest("items", pageResult);
		render("clearing/collect/collectRiskList.html");
	}

	public void collectFreezePage() {
		List<Channel> channelList = Channel.dao.selectAll();
		setAttr("channelList", channelList);
		render("clearing/collect/collectFreeze.html");
	}

	public void collectFreeze() {
		String channelCode = getPara("channelCode");
		String orgCode = getPara("orgCode");
		String merchCode = getPara("merchCode");
		String freezeAmountStr = getPara("freezeAmount");
		BigDecimal freezeAmount = new BigDecimal(freezeAmountStr).multiply(new BigDecimal("100"));
		ClearingCollect collect = new ClearingCollect();
		collect.setChannelCode(channelCode);
		collect.setOrgCode(orgCode);
		collect.setMerchCode(merchCode);
		collect.setFreezeAmount(freezeAmount.longValue());
		collect.setId(FSIDGenerate.getUUID());
		collect.setOrderNo(FSIDGenerate.nextOrderNo());
		collect.setSumCount(0L);
		collect.setAmount(0L);// 已冻结金额
		collect.setFee(0L);
		collect.setPaymentAmount(0L);
		collect.setLastFreezeAmount(0L);
		collect.setCreateDate(FSDate.getDate());
		collect.setCreateTime(FSDate.getDateTime());
		collect.setFreezeTime(collect.getCreateTime());
		collect.setPaymentStatus(SystemEnum.PaymentStatus.Freeze.getValue());
		collect.setIsRisk(true);
		collect.save();
		renderSuccess();
	}

	public void collectThawPage() {
		ClearingCollect clearingCollect = ClearingCollect.dao.findById(getPara("id"));
		List<Channel> channelList = Channel.dao.selectAll();
		setAttr("channelList", channelList);
		setAttr("clearingCollect", clearingCollect);
		render("clearing/collect/collectThaw.html");
	}

	public void collectThaw() {
		ClearingCollect clearingCollect = getModel(ClearingCollect.class);
		clearingCollect.setPaymentAmount(clearingCollect.getAmount());
		clearingCollect.setPaymentStatus(SystemEnum.PaymentStatus.Thaw.getValue());
		clearingCollect.setThawTime(FSDate.getDateTime());
		clearingCollect.setCreateDate(FSDate.getDate());
		clearingCollect.update();
		renderSuccess();
	}
	
	
	public void searchCollect() {
		Page<WxClear> pageResult = WxClear.dao.search(super.getSearchCondition());
//		List<Object> list = ClearingCollect.dao.searchSum(super.getSearchCondition());
//		setAttr("sumCount", "0");
//		setAttr("sumAmount", "0");
//		setAttr("sumFee", "0");
//		setAttr("sumPaymentAmount", "0");
//		if (list.size() > 0) {
//			for (Object o : list) {
//				Object[] value = (Object[]) o;
//				setAttr("sumCount", value[0]);
//				if (value[1] != null) {
//					setAttr("sumAmount", Long.parseLong(value[1].toString()));
//				}
//				if (value[2] != null) {
//					setAttr("sumFee", Long.parseLong(value[2].toString()));
//				}
//				if (value[3] != null) {
//					setAttr("sumPaymnetAmount", Long.parseLong(value[3].toString()));
//				}
//			}
//		}
		setPageResultToRequest("items", pageResult);
		render("clearing/collect/wxClearList.html");
	}
	

	public void searchCollect_old() {
		Page<ClearingCollect> pageResult = ClearingCollect.dao.search(super.getSearchCondition());
		List<Object> list = ClearingCollect.dao.searchSum(super.getSearchCondition());
		setAttr("sumCount", "0");
		setAttr("sumAmount", "0");
		setAttr("sumFee", "0");
		setAttr("sumPaymentAmount", "0");
		if (list.size() > 0) {
			for (Object o : list) {
				Object[] value = (Object[]) o;
				setAttr("sumCount", value[0]);
				if (value[1] != null) {
					setAttr("sumAmount", Long.parseLong(value[1].toString()));
				}
				if (value[2] != null) {
					setAttr("sumFee", Long.parseLong(value[2].toString()));
				}
				if (value[3] != null) {
					setAttr("sumPaymnetAmount", Long.parseLong(value[3].toString()));
				}
			}
		}
		setPageResultToRequest("items", pageResult);
		render("clearing/collect/collectList.html");
	}

	public void viewCollect() {
		ClearingCollect clearingCollect = ClearingCollect.dao.findById(getPara("id"));
		setAttr("channelList", Channel.dao.selectAll());
		setAttr("clearingCollect", clearingCollect);
		render("clearing/collect/collectView.html");
	}

}
